package main

import _ "github.com/go-sql-driver/mysql" 
import "database/sql"
import "github.com/gin-gonic/gin"
import "net/http"
import "fmt"
import "log"

const (
    USERNAME = "root"
    PASSWORD = "wyqtthh-=45"
    NETWORK  = "tcp"
    SERVER   = "localhost"
    PORT     = 3306
    DATABASE = "test"
    CHARSET  = "utf8"
)

type OrderList struct{
    Id 			string	`json:"id"`
    Broker 		string	`json:"broker"`
    Address		string	`json:"address"`
    Pattern		string	`json:"pattern"`
    Operator	string	`json:"operator"`
    Booking		string	`json:"booking"`  
    Diy_type	string	`json:"diy_type"`
    Suburb		string	`json:"suburb"`
    Desc		string	`json:"desc"`
    Degree		string	`json:"degree"`
    Dateline	sql.NullString  `json:"dateline,-"`
    Satisfied	string	`json:"satisfied"`
    Price		string	`json:"price"`
    Reason		string	`json:"reason"`
    Feedback	string	`json:"feedback"`
    Fee			string	`json:"fee"`
    Validate	string	`json:"validate"`
}

func main() {
	// 初始化框架
	r := gin.Default()
	r.Delims("<{", "}>")
	r.Static("/static", "./static")
	r.LoadHTMLGlob("templates/*")

	// A页面渲染
	r.GET("/homepage", homepage)
	r.GET("/savepage", savepage)

	// B数据交互
	r.GET("/getlist", getlist)
	r.GET("/getinfo", getinfo)
	r.GET("/delorder", getlist)
	r.POST("/addinfo", saveinfo)
	r.POST("/saveinfo", saveinfo)

	r.Run(":8090")
}


func homepage(c *gin.Context) {
	c.HTML(http.StatusOK, "index.html", gin.H{"title":"Index"})
}

func savepage(c *gin.Context) {
	idStr := c.Query("id")

	title := "添加订单"
	if idStr != "" {
		title = "数据修改"
	}else{
		idStr = ""
	}

	c.HTML(http.StatusOK, "edit.html", gin.H{"title":title,"orderid":idStr})
}

func getlist(c *gin.Context) {
	dsn := fmt.Sprintf("%s:%s@%s(%s:%d)/%s?charset=%s", USERNAME, PASSWORD, NETWORK, SERVER, PORT, DATABASE, CHARSET)
	DB, err := sql.Open("mysql", dsn)
	if err != nil{
		c.JSON(http.StatusOK, gin.H{"errcode":"0","message":"数据库连接失败"})
		return
	}

	stmt,err:=DB.Prepare("SELECT id,broker,address,pattern,operator,booking,diy_type,suburb,`desc`,degree,dateline,satisfied,price,reason,feedback,fee,validate FROM work_order where fee =?")
	if err != nil{
		c.JSON(http.StatusOK, gin.H{"errcode":"0","message":"数据库SQL预处理失败"})
		return
	}

	rows ,err :=stmt.Query("100")
	if err != nil{
		c.JSON(http.StatusOK, gin.H{"errcode":"0","message":"数据查询失败"})
		return
	}

	defer DB.Close()
	defer stmt.Close()
	defer rows.Close()

	result := []OrderList{}
	for rows.Next() {
		var order OrderList
		err :=rows.Scan(&order.Id,&order.Broker,&order.Address,&order.Pattern,&order.Operator,&order.Booking,&order.Diy_type,&order.Suburb,&order.Desc,&order.Degree,&order.Dateline,&order.Satisfied,&order.Price,&order.Reason,&order.Feedback,&order.Fee,&order.Validate)
		checkErr(err)

		// 填充数组
		result = append(result, order)
	}

	c.JSON(http.StatusOK, gin.H{"errcode":"0","message":"success","data":result})
}

func getinfo(c *gin.Context) {
	idStr := c.Query("id")
	if idStr == ""{
		c.JSON(http.StatusOK, gin.H{"errcode":"0","message":"缺少必要参数"})
		return
	}

	dsn := fmt.Sprintf("%s:%s@%s(%s:%d)/%s?charset=%s", USERNAME, PASSWORD, NETWORK, SERVER, PORT, DATABASE, CHARSET)
	DB, err := sql.Open("mysql", dsn)
	if err != nil{
		c.JSON(http.StatusOK, gin.H{"errcode":"0","message":"数据库连接失败"})
		return
	}

	//stmt,err:=DB.Prepare("SELECT id,broker,address,pattern,operator,booking,diy_type,suburb,`desc`,degree,dateline,satisfied,price,reason,feedback,fee,validate FROM work_order where id =?")
	var order OrderList
	err = DB.QueryRow(
		"SELECT id,broker,address,pattern,operator,booking,diy_type,suburb,`desc`,degree,dateline,satisfied,price,reason,feedback,fee,validate FROM work_order where id =?",
		 idStr).Scan(
		   &order.Id,&order.Broker,&order.Address,&order.Pattern,&order.Operator,&order.Booking,&order.Diy_type,&order.Suburb,&order.Desc,&order.Degree,&order.Dateline,&order.Satisfied,&order.Price,&order.Reason,&order.Feedback,&order.Fee,&order.Validate,
		  )

	if err != nil{
		log.Println(err)
		c.JSON(http.StatusOK, gin.H{"errcode":"0","message":"没有相关数据"})
		return
	}

	c.JSON(200, gin.H{"errcode":0,"message":"success", "data":order})
}

func saveinfo(c *gin.Context) {
	c.JSON(200, gin.H{"message":"success"})
}

func delorder(c *gin.Context) {
	c.JSON(200, gin.H{"message":"success"})
}

func checkErr(err error){
	if err!=nil{
		panic(err)
	}
}




